//
//  JourneyViewController.m
//  QYer
//
//  Created by lanou on 16/8/10.
//  Copyright © 2016年 CaiZhuYu. All rights reserved.
//

#import "JourneyViewController.h"
#import "JourneyTopCell.h"

@interface JourneyViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation JourneyViewController

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)requestData {
    NSString *urlString = @"http://open.qyer.com/qyer/home/home_feed?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&oauth_token=64ae5bc0d023f71444c8f00f096eae24&page=1&track_app_channel=App%2520Store&track_app_version=7.0&track_device_info=iPhone%25205%28ChinaMobile%2CChinaTelecom%2CChinaUnicom%29&track_deviceid=2D9E8D6C-2140-49DC-993D-D66133CC22C0&track_os=ios%25209.3.2&track_user_id=8252405&v=1";
    [NetWorkTool requstWithType:GET URLString:urlString parameter:nil callBack:^(NSData *data, NSError *error) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dic in jsonDic[@"data"][@"feed"][@"entry"]) {
            JourneyTopModel *model = [[JourneyTopModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        NSLog(@"%@", self.dataArray);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }];
}

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"JourneyTopCell" bundle:nil] forCellReuseIdentifier:@"reuse"];
}


#pragma mark -----

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    
    [self requestData];
    [self createTableView];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


#pragma mark ----- UITableViewDataSource -----

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JourneyTopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    JourneyTopModel *model = self.dataArray[indexPath.row];
    [cell setContentWithModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 320;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //Q-TOP
    //http://m.qyer.com/bbs/thread-2568632-1.html?track_app_version=7.0
    //发现下一站
    //http://m.qyer.com/guide/page/As_HZ6Y9_IY/?lk=1
    //http://m.qyer.com/guide/page/AtAQkKIMszs/?lk=1
    //http://m.qyer.com/guide/page/4QQWm2LX6Ok/?lk=1
    //最会买
    //http://m.qyer.com/guide/page/w1yu0dknUTs/?lk=1
    
}







@end
